package com.cyn.controller.handler;


import java.io.*;

public class Test {

    public static void main(String[] args) throws IOException {

        String dd = fileToBase64("C:\\Users\\lenovo\\Desktop\\阜阳\\0_task\\电子证照\\对接文档\\对接文档\\0. 电子证照安全接口对接文档.pdf");
        System.out.println(dd);
    }


    public static String fileToBase64(String filepath) throws IOException {
        File file = null;
        long len;
        byte[] bytes = null;
        BufferedInputStream bis = null;

        try {

            file = new File(filepath);
            if (!file.exists()) {
                throw new FileNotFoundException("文件不存在");
            }

            len = file.length();
            bytes = new byte[(int) len];
            bis = new BufferedInputStream(new FileInputStream(file));
            int r = bis.read(bytes);
            if (r != len)
                throw new IOException("文件读取出错");

            bis.close();
        } finally {
            if (bis != null) {
                bis.close();
            }
        }

        return Base64.encode(bytes);
    }
}
